package 测试;

public class 异常测试 {
	public static void main(String[] args) {
		System.out.println(testFinally_1());
		System.out.println(testFinally_2().toString());
	}

	/**
	 * 在finally中改变值类型的变量，不能改变，因为try返回时销毁栈中的常量
	 * 
	 * @return
	 */
	public static int testFinally_1() {
		int res = 1;
		try {
			res = 2;
			return res;
		} catch (Exception e) {
			return 0;
		} finally {
			res = 3;
			System.out.println("fianlly_1");
		}
	}

	/**
	 * StringBuilder是引用类型存储在堆中，return的是引用的拷贝，所以finally可以被改变
	 * 
	 * @return
	 */
	public static StringBuilder testFinally_2() {
		StringBuilder sb = new StringBuilder().append("Hello ");
		try {
			sb.append("AAA ");
			return sb;
		} catch (Exception e) {
			return null;
		} finally {
			sb.append("BBB");
			System.out.println("fianlly_1");
		}
	}
}
